//
//  NewsSubViewController.swift
//  AirTravel
//
//  Created by apple on 2021/11/9.
//

import UIKit

class NewsSubViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return model?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsSubTableViewCell", for: indexPath) as! NewsSubTableViewCell
        let tap=UITapGestureRecognizer.init()
        
        tap.addTarget(self, action: #selector(tapClick(sender:)))
        cell.contentView.addGestureRecognizer(tap)
        cell.selectionStyle = .none
        cell.title.text = model![indexPath.section].name
        
        cell.time.text = model![indexPath.section].create_time
        
        cell.review.text = model![indexPath.section].nums + "次浏览"
        
        cell.img.kf.setImage(with: URL(string: model![indexPath.section].image))
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.init(hexString: "F7F7F7")
        return view
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsContentViewController") as! NewsContentViewController
        vc.newsID = model![indexPath.section].id
        vc.title = model![indexPath.section].name
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    

    @IBOutlet weak var tab: UITableView!
    
    var model:[NewsDetailModel]?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.isNavigationBarHidden = true
        // Do any additional setup after loading the view.
       
        tab.delegate = self
        tab.dataSource = self
        tab.reloadData()
        tab.removeMoreLine()
    }
    
    

    @objc func tapClick(sender:UITapGestureRecognizer){
        
        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsContentViewController") as! NewsContentViewController
        vc.newsID = model![sender.view?.tag ?? 0].id
        vc.name = model![sender.view?.tag ?? 0].name
        self.navigationController?.pushViewController(vc, animated: true)
        
//        print("\(sender.view?.tag)")
    }
}
